# 导入配置到 etcd
config:
	etcdctl put "/ebike/config/ota"  < ./script/config.json

# 部署测试环境
# make deploy env=test
#
group = ebike_registry
project = ota
version = v1.1.0

# 默认值
group = ebike_registry
hub_user = 深圳市豆点云科技有限公司
hub_pwd = dev%ddyk@219_i
server = registry.cn-hangzhou.aliyuncs.com

hub_user_prod = aliyun6128162341
server_prod = registry.cn-shenzhen.aliyuncs.com
group_prod = smartemc_registry

remote_host_dev = "api.dev.ddyunche.com"
remote_host_test = "api.test.ddyunche.com"
remote_host = ${remote_host_dev}

env ?= dev
remote_docker_compose_path = "/root/deploy/docker_compose/local"

# Define ANSI escape codes for colors
COLOR_RESET = \033[0m
COLOR_BRIGHT_MAGENTA = \033[35;1m

# 条件判断，根据不同的 env 设置相应的值
ifeq ($(env), test)
	remote_host = ${remote_host_test}
else ifeq ($(env), prod)
	hub_user = ${hub_user_prod}
	server = ${server_prod}
	group = ${group_prod}
else ifeq ($(env), dev)
	remote_host = ${remote_host_dev}
endif

default:
	@echo ${group}/${project}

build: #执行 make -f makefile build
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64  go build -gcflags="-N -l" -buildvcs=false -o ./bin/${project}

image:build
	docker build -t ${group}/${project}:${version} --platform linux/amd64 --build-arg ARG_PROJECT_NAME=${project} .

push:image
	docker login -u ${hub_user} -p ${hub_pwd} ${server}
	docker tag ${group}/${project}:${version} ${server}/${group}/${project}:${version}
	docker push ${server}/${group}/${project}:${version}

deploy:image
#	@echo "当前部署的服务器信息:"
#	@echo "env: $(env)"
#	@echo "remote_host: $(remote_host)"
	@echo "${COLOR_BRIGHT_MAGENTA}当前部署的服务器信息:${COLOR_RESET}"
	@echo "${COLOR_BRIGHT_MAGENTA}env: $(env)${COLOR_RESET}"
	@echo "${COLOR_BRIGHT_MAGENTA}remote_host: $(remote_host)${COLOR_RESET}"
	docker tag ${group}/${project}:${version} ${server}/${group}/${project}:${version}
	docker save ${server}/${group}/${project}:${version} -o ${server}-${group}-${project}-${version}.tar
	scp ./${server}-${group}-${project}-${version}.tar root@${remote_host}:/tmp
	ssh root@${remote_host} "echo y | docker image prune && \
						 docker logout && \
						 docker rm -f ebike_ota && \
						 docker image rm -f ${server}/${group}/${project}:${version} && \
						 docker load -i /tmp/${server}-${group}-${project}-${version}.tar && \
						 cd ${remote_docker_compose_path} && \
						 docker compose down ${project} && \
						 docker compose up -d ${project} && \
						 rm -f /tmp/${server}-${group}-${project}-${version}.tar"
	rm -rf ./${server}-${group}-${project}-${version}.tar

container:image
	docker rm -f ${group}_${project}
	docker run --name ${group}_${project} -d --net host --restart always -e MICRO_REGISTRY=etcd -e MICRO_REGISTRY_ADDRESS=etcd:2379 ${group}/${project}

.PHONY: defaul build image push container
